<?php 
class Country_PhotoController extends Core_Controller_Action_Standard
{
     public function init() {
		if (!$this->_helper->requireAuth()
			->setAuthParams('album', null, 'view')
			->isValid()) {
			return;
		}
		$this->_helper->layout()
			->disableLayout(true);
	}

	public function indexAction() {
		$this->view->someVar = 'someVal';
	}

	public function viewAction() {
		$href = $this->_getParam('link');
		if (!preg_match('/^album_id\/([0-9]*)\/photo_id\/([0-9]*)/i', $href, $matches))
			$this->_helper->viewRenderer->setNorender(true);
	
		// Get message id
		$message_id = 0;
		if (preg_match('/message\/([0-9]+)/i', $href, $msgMatches)) {
			$message_id = $msgMatches[1];
		}
        
        $country_id = 0;
        if (preg_match('/cid\/([0-9]+)/i', $href, $cuntryMatches)) {
			$country_id = $cuntryMatches[1];
		}
        $country = Engine_Api::_()->getItem('country',$country_id);
        if (!$country){
       	    $this->_helper->viewRenderer->setNorender(true);
        }
        $this->view->country = $country;
       
		$this->view->album = $album = Engine_Api::_()->getItem('album', $matches[1]);
		$this->view->photo = $photo = Engine_Api::_()->getItem('photo', $matches[2]);
		$this->view->status = false;
		$this->view->viewer = $viewer = Engine_Api::_()->user()
			->getViewer();
            
        
		if ($album && $photo && $album->album_id == $photo->album_id) {
			
			$this->view->status = true;
			if (!$viewer || !$viewer->getIdentity() || !$album->isOwner($viewer)) {
				$photo->view_count = new Zend_Db_Expr('view_count + 1');
				$photo->save();
			}
			// if this is sending a message id, the user is being directed from a coversation
			// check if member is part of the conversation
			

			$message_view = false;
			if ($message_id && !empty($message_id) && is_numeric($message_id)) {
				
				$conversation = Engine_Api::_()->getItem('messages_conversation', $message_id);
				
				if ($conversation->hasRecipient(Engine_Api::_()->user()
					->getViewer()))
					$message_view = true;
			}
			$this->view->message_view = $message_view;
			
			//if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'view')->isValid() ) return;
			if (!$message_view && !$this->_helper->requireAuth()
				->setAuthParams($photo, null, 'view')
				->isValid())
				return;
			
			$this->view->canEdit = $canEdit = $album->authorization()
				->isAllowed($viewer, 'edit');
			$this->view->canDelete = $canDelete = $album->authorization()
				->isAllowed($viewer, 'delete');
			$this->view->canTag = $canTag = $album->authorization()
				->isAllowed($viewer, 'tag');
			$this->view->canUntagGlobal = $canUntag = $album->isOwner($viewer);
			
			$this->view->nextPhoto = $photo->getNextPhoto();
			$this->view->previousPhoto = $photo->getPreviousPhoto();
			
			// Get tags
			$tags = array ();
			foreach ($photo->tags()
				->getTagMaps() as $tagmap) {
				$tags[] = array_merge($tagmap->toArray(), array (
					'id' => $tagmap->getIdentity(), 
					'text' => $tagmap->getTitle(), 
					'href' => $tagmap->getHref(), 
					'guid' => $tagmap->tag_type . '_' . $tagmap->tag_id
				));
			}
			$this->view->tags = $tags;
			$this->view->photoOwner = $photo->getOwner();
		}
	}

  
}